package com.example.demo.controller;

import com.example.demo.common.Result;
import com.example.demo.entity.Inventory;
import com.example.demo.service.InventoryMyBatisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 库存MyBatis控制器
 */
@RestController
@RequestMapping("/api/mybatis/inventory")
public class InventoryMyBatisController {
    
    @Autowired
    private InventoryMyBatisService inventoryMyBatisService;
    
    /**
     * 查询库存不足的商品
     */
    @GetMapping("/low-stock")
    public Result<List<Inventory>> findLowStockInventories(@RequestParam(defaultValue = "10") Integer threshold) {
        List<Inventory> inventories = inventoryMyBatisService.findLowStockInventories(threshold);
        return Result.success(inventories);
    }
    
    /**
     * 根据仓库ID查询库存
     */
    @GetMapping("/warehouse/{warehouseId}")
    public Result<List<Inventory>> findByWarehouseId(@PathVariable Long warehouseId) {
        List<Inventory> inventories = inventoryMyBatisService.findByWarehouseId(warehouseId);
        return Result.success(inventories);
    }
    
    /**
     * 根据商品ID查询所有仓库的库存
     */
    @GetMapping("/product/{productId}")
    public Result<List<Inventory>> findByProductId(@PathVariable Long productId) {
        List<Inventory> inventories = inventoryMyBatisService.findByProductId(productId);
        return Result.success(inventories);
    }
    
    /**
     * 统计总库存价值
     */
    @GetMapping("/total-value")
    public Result<Double> getTotalInventoryValue() {
        Double totalValue = inventoryMyBatisService.getTotalInventoryValue();
        return Result.success(totalValue);
    }
    
    /**
     * 根据仓库统计库存价值
     */
    @GetMapping("/value-by-warehouse/{warehouseId}")
    public Result<Double> getInventoryValueByWarehouse(@PathVariable Long warehouseId) {
        Double value = inventoryMyBatisService.getInventoryValueByWarehouse(warehouseId);
        return Result.success(value);
    }
    
    /**
     * 查询即将过期的库存
     */
    @GetMapping("/expiring-soon")
    public Result<List<Inventory>> findExpiringSoon(@RequestParam(defaultValue = "30") Integer days) {
        List<Inventory> inventories = inventoryMyBatisService.findExpiringSoon(days);
        return Result.success(inventories);
    }
    
    /**
     * 批量更新库存数量
     */
    @PutMapping("/batch-update")
    public Result<String> updateStockBatch(@RequestBody List<Inventory> inventories) {
        int updated = inventoryMyBatisService.updateStockBatch(inventories);
        return Result.success("成功更新 " + updated + " 个库存记录");
    }
    
    /**
     * 查询库存变动记录
     */
    @GetMapping("/movements")
    public Result<List<Inventory>> findStockMovements(
            @RequestParam Long productId, 
            @RequestParam(required = false) Long warehouseId) {
        List<Inventory> inventories = inventoryMyBatisService.findStockMovements(productId, warehouseId);
        return Result.success(inventories);
    }
}
